﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using RevitArchiFunctions.YJKView;


namespace %BaseClassName%
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class %Class% : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			this.m_app = cmdData.Application.Application;
			
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
			this.m_Doc = m_uiDoc.Document;
			this.m_selection = m_uiDoc.Selection;
			
			RevitVersionFuncs.ClearSelection(this.m_selection);
			if (SwitchView.SetUsableView(CmdViewType.ThreeD, cmdData) != Result.Succeeded)
            {
                return Result.Failed;
            }
			
			if (%Class%._hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				%Class%._hWndRevit = new WindowHandle(mainWindowHandle);
			}
			
			%Project% form = new %Project%(cmdData);
			form.Show(%Class%._hWndRevit);
			
			int wMsg = 7;
			%Class%.SendMessage(%Class%._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			
			Result result;
			for (;;)
			{
				try
				{
					Reference reference = this.m_selection.PickObject(ObjectType.Element, new AxisFilter(), "请选择轴线:");
				}
				catch (Exception)
				{
					form.Close();
					result = Autodesk.Revit.UI.Result.Succeeded;
					break;
				}
			}
			return result;
		}

		public static WindowHandle _hWndRevit;
		
		private UIDocument m_uiDoc;
		private Document m_Doc;
		private Application m_app;
		private Selection m_selection;
	}
}
